﻿using System;
using System.Windows;
using Kiva7.DataRequests;
using Kiva7.Utilities;

namespace Kiva7.Pages
{
    public partial class CommentsPage
    {
        private const string JournalEntryIdKey = "journalEntryId";

        private const string Url = "/Pages/CommentsPage.xaml";
        public readonly static Uri PageUrl = new Uri(Url, UriKind.RelativeOrAbsolute);

        public static Uri GetPageUrl(int journalEntryId)
        {
            UrlCreator url = new UrlCreator(Url);
            url.Add(JournalEntryIdKey, journalEntryId);

            return url.ToUri(UriKind.RelativeOrAbsolute);
        }

        public CommentsPage()
        {
            InitializeComponent();
            CreateStandardControls();
        }

        CommentsRequest request;

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (request == null)
            {
                int journalEntryId;
                if (int.TryParse(NavigationContext.QueryString[JournalEntryIdKey], out journalEntryId))
                {
                    request = new CommentsRequest();
                    //request.OnResponse += new DataRequest<DataResponse.CommentsResponse, Comment>.DataRequestDelegate(request_OnResponse);
                    DataContext = request;
                    request.GetComment(journalEntryId);

                }
            }
        }

        //void request_OnResponse(object sender, DataRequest<DataResponse.CommentsResponse, Comment>.DataRequestEventArgs e)
        //{
        //    if (e.Response == null
        //        || e.Response.Comments == null
        //        || e.Response.Comments.CommentItems == null
        //        || e.Response.Comments.CommentItems.Count == 0)
        //    {
        //        DataContext = null;
        //    }
        //    else
        //    {
        //        DataContext = e.Response.Comments;
        //    }
        //}
    }
}